/*
 * qspi.c
 *
 *  Created on: Apr 8, 2013
 *      Author: Vincent
 */

#include "support_common.h" /* include peripheral declarations and more */
#include <stdio.h>
#include "qspi.h"

uint8 g_cmd =0;

void qspi_init()
{	
	// Configure port QS pins 0, 2 for the primary (QSPI) function
    MCF_GPIO_PQSPAR |= 0x11; // sets PQSPAR2, PQSPAR0 to 01 which is primary function 
    
    //Set QSPI to master mode
    MCF_QSPI_QMR |= 0x8000; // sets MSB of 16 bit QMR reg
    
    // set the number of bits to send
    MCF_QSPI_QMR |= 0x2000; //sets bit 13 of QMR meaning 8 bits are sent at a time. 16 will be sent total into the shift for both column and row.
    
    // set polarity and phase
    MCF_QSPI_QMR &= ~(0x0300); // set pol to 0 and phase 0
    
    //set BAUD rate to 
    MCF_QSPI_QMR |= 0x08; // set to 80 so 80MHz/(2*8) = 5000kHz for 5000Mbps
    
    // Disable qspi halting, wraparound+location, CCSIV, ENDQP, NEWQP;
    MCF_QSPI_QWR &= 0x0000; // clears MSB of 16 bit QWR reg
    
    
    // Disables QSPI enable 
    //MCF_QSPI_QDLYR &= 0 << 15; 
    // global variable set to 0x4000?
    
    // Disable interrupts and clear all interrupt flags for QSPI
    MCF_QSPI_QIR &= 0x0000;
    MCF_QSPI_QIR |= 0x0101;
}

void qspi_xfer(uint8 data[])
{	
	for (int i =0; i<3; i++)
		{
			// SET THE ADDRESS OF DATA INDEX TO BE IMPLEMENTED?
			MCF_QSPI_QAR  = (unsigned short)i;
			// Send data at index I into DR
			MCF_QSPI_QDR  = (unsigned short) data[i]; 
		}
	for (int i=0; i<3; i++)
		{
			// SET THE ADDRESS OF DATA INDEX TO BE IMPLEMENTED?
			MCF_QSPI_QAR  = (unsigned short) (0x20 + i); 
			
			MCF_QSPI_QDR  = (unsigned short) 0x0000; 
		}
	/*for (int i =0; i<8; i++)
	{
		// SET THE ADDRESS OF DATA INDEX TO BE IMPLEMENTED?
		MCF_QSPI_QAR  = i;
		// Send data at index I into DR
		MCF_QSPI_QDR  = Led_buff[i]; 
	}
	
	for (int i=0; i<8; i++)
	{
		// SET THE ADDRESS OF DATA INDEX TO BE IMPLEMENTED?
		MCF_QSPI_QAR  = 0x20 + i; 
		
		MCF_QSPI_QDR  = 0x4F00; 
	}
	*/
	// set que address to 0
	MCF_QSPI_QWR &= 0xFFF0;
	
	// Set end of data address		
	MCF_QSPI_QWR |= 0x0200; //MCF_QSPI_QWR |= 0x07;
	
	// Set MSB of DLR to start transfer
	
	MCF_QSPI_QDLYR |= 0x8000; 
		
	while ((MCF_QSPI_QIR & 0x0001) == 0)
	{
	
	}
	
	MCF_QSPI_QIR |= 0x0001;
}	

//NOT NEEDED?
void qspi_xrec()
{}
